package com.app.micaihu.view.main.dialog

import android.content.DialogInterface
import androidx.recyclerview.widget.GridLayoutManager
import com.app.base.BaseDialogFragment
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.ConfigKeyConstant
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.utils.ImageLoadUtils
import com.app.micaihu.view.bean.Military
import com.app.micaihu.view.main.dialog.adapter.MilitaryAdapter
import com.app.micaihu.view.utils.DownloadAgainUtils
import com.app.utils.biz.Biz
import com.app.utils.util.DateUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.StringUtils
import kotlinx.android.synthetic.main.mian_no_login_dialog.*

class MainNoLoginDialogFragment : BaseDialogFragment() {
    override fun getLayoutId(): Int {
        return R.layout.mian_no_login_dialog
    }

    private val militaryAdapter: MilitaryAdapter by lazy {
        val militaryAdapter = MilitaryAdapter()
        val gridLayoutManager = GridLayoutManager(context, 3)
        rvMilitary.layoutManager = gridLayoutManager
        rvMilitary.adapter = militaryAdapter
        val list = initData()
        militaryAdapter.setNewInstance(list)
        militaryAdapter
    }


    override fun setData() {
        ImageLoadUtils.loadImageNoScaleType(R.mipmap.main_login_huniu, ivHuniu)
    }

    override fun addListeners() {
        tvOk.setOnClickListener {
            dismiss()
            UserInforManager.getInstance().toLoginPage(activity)
        }
        ivClose.setOnClickListener { dismiss() }

        militaryAdapter.setOnItemClickListener { adapter, view, position ->
            val item = militaryAdapter.getItem(position)
            militaryAdapter.setCheck(position)
            Biz.instance.military = item.id.toString()
        }
    }

    private fun initData(): ArrayList<Military> {
        val list = arrayListOf<Military>()
        val military = Biz.instance.military
        var militaryIndex = 1
        if (military.isNotEmpty()) {
            militaryIndex = military.toInt()
        }
        for ((index, item) in Config.militaryList.withIndex()) {
            val military: Military
            if (militaryIndex == index + 1) {
                military = Military(index + 1, item, Config.militaryIconList[index], true)
                Biz.instance.military = (index + 1).toString()
            } else {
                military = Military(index + 1, item, Config.militaryIconList[index], false)
            }
            list.add(military)
        }
        return list
    }

    override fun onDismiss(dialog: DialogInterface) {
        val currentTime = DateUtils.getCurrentTime(DateUtils.formatDefault)
        val isShowPopupDialogData = SPUtils.getInstance().getString(ConfigKeyConstant.isShowPopupDialogData)
        if (currentTime == isShowPopupDialogData) return
        val popupImg = SPUtils.getInstance().getString(ConfigKeyConstant.popupImg, "")
        val popupStatus = SPUtils.getInstance().getString(ConfigKeyConstant.popupStatus, "")
        if (!StringUtils.isEmpty(popupImg) && "1" == popupStatus) {
            val mainPopupDialogFragment = MainPopupDialogFragment()
            mainPopupDialogFragment.show(parentFragmentManager)
        } else {
            DownloadAgainUtils.showDownLoadDialog(parentFragmentManager)
        }
        super.onDismiss(dialog)
        SPUtils.getInstance().put(ConfigKeyConstant.isShowNoLoginDialogData, DateUtils.getCurrentTime(DateUtils.formatDefault))

    }
}